package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import data.Event;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents displaying event information  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */
public class ShowEvent extends ViewPanel
{
	private JLabel title;
    private JLabel eventName;
    private JLabel eventDate;
    private JLabel eventTime;
    private JLabel eventRec;
    private JTextArea eventDesc;
    
    private static final String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    
    /**
     * Construct ViewPanel containing JComponents displaying event information.
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
	public ShowEvent(final ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        Dimension labelSize = new Dimension(150, 20);
        int labelX = 60;
        int labelY = 40;
        int labelSpread = 30;
        int spaceX = labelX + (int)labelSize.getWidth() + 20;
        
        title = new JLabel();
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        JLabel nameLabel = new JLabel("Name: ");
        nameLabel.setSize(labelSize);
        nameLabel.setLocation(labelX, labelY);
        this.add(nameLabel);
        
        eventName = new JLabel();
        eventName.setSize(labelSize);
        eventName.setLocation(spaceX, labelY);
        labelY += labelSpread;
        this.add(eventName);
        
        JLabel meetingLabel = new JLabel("Date: ");  //date label    
        meetingLabel.setSize(labelSize);
        meetingLabel.setLocation(labelX, labelY);
        this.add(meetingLabel);
        
        eventDate = new JLabel();
        eventDate.setSize(labelSize);
        eventDate.setLocation(spaceX, labelY);
        labelY += labelSpread;
        this.add(eventDate);
        
        JLabel startTimeLabel = new JLabel("Time: ");
        startTimeLabel.setSize(labelSize);
        startTimeLabel.setLocation(labelX, labelY);
        this.add(startTimeLabel);
        
        eventTime = new JLabel();
        eventTime.setSize(labelSize);
        eventTime.setLocation(spaceX, labelY);
        labelY += labelSpread;
        this.add(eventTime);
        
        JLabel recursiveLabel = new JLabel("Every: ");
        recursiveLabel.setSize(labelSize);
        recursiveLabel.setLocation(labelX, labelY);
        this.add(recursiveLabel);
        
        eventRec = new JLabel();
        eventRec.setSize(500, 20);
        eventRec.setLocation(spaceX, labelY);
        labelY += labelSpread;
        this.add(eventRec);
        
        JLabel descLabel = new JLabel("Description: ");
        descLabel.setSize(labelSize);
        descLabel.setLocation(labelX, labelY);
        this.add(descLabel);        
        
        eventDesc = new JTextArea();
        eventDesc.setSize(350,120);
        eventDesc.setLocation(spaceX, labelY);
        labelY += labelSpread;
        this.add(eventDesc);
        eventDesc.setEditable(false);
        
        JButton backButton = new JButton("Back");
        backButton.setSize(100,20);
        backButton.setLocation(200, 400);
        backButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                viewManager.showView("calendar");
                
            }
            
        });
        this.add(backButton);
    }

	/**
	 * Sets the information of the event into the JComponents
	 * @param ev the event whose information is displayed
	 */
	public void setEvent(Event ev)
	{
	    title.setText("View " + ev.getType());
		String date = (ev.getStartTime().get(Calendar.MONTH)+1) + "/" + 
				(ev.getStartTime().get(Calendar.DAY_OF_MONTH)+1) + "/" + ev.getStartTime().get(Calendar.YEAR);
		eventName.setText(ev.getEventName());
		eventDate.setText(date);
		String time = ev.getStartTimeString();
		if (!ev.getType().equals("deadline"))
		    time += "-" + ev.getEndTimeString();
		eventTime.setText(time);
		boolean[] recurrence = ev.getDaysOfWeek();
		String rec = "";
	    for (int i = 1; i < recurrence.length; i++)
	        rec += recurrence[i] ? " " + days[i-1] : "";
		
		eventRec.setText(rec);
		eventDesc.setText(ev.getDescription());
	}
	
	 /**
     * Clears all text fields and check boxes
     */
	public void clearView()
	{
		this.title.setText("Untitled");
        eventName.setText("");
        eventDate.setText("");
        eventTime.setText("");
        eventRec.setText("");
        eventDesc.setText("");
	}
}
